// import React,{Component} from "react";

// class Player extends Component{
//     constructor(props){
//         super(props);
//         this.state={
//             playStatus:"PLAY",
//             isPlaying:true,
//         }
//         this.togglePlay=this.togglePlay.bind(this);
//     }
//     componentDidUpdate(prevState,prevProps){
//         this.audio.src=this.props.src;
//         setTimeout(()=>{
//             this.audio.play();
//         },500);
//     }
//     componentDidMount(){
//         this.audio=new Audio(this.props.src);
//         this.audio.play();
//     }
//     togglePlay(){
//         this.audio[this.state.isPlaying?"pause":"play"]();
//         this.setState({
//             isPlaying:!this.state.isPlaying,
//         });
//     }
//     render(){
//         return (<div>
//             播放:{this.props.name}
//             <span onClick={this.togglePlay}>{this.state.isPlaying?"暂停":"播放"}</span>
//         </div>)
//     }
// }

// export default Player;

const Player=new Audio();

function togglePlay(){
    const isPaused=Player.paused;
    Player[isPaused?"play":"pause"]();
    return !Player.paused;
}
function changeSong(src){
    Player.src=src;
    setTimeout(()=>{
        Player.play();
    },500);
}
function addUpdateCallback(){
    let updateCallback=null;
    Player.addEventListener('timeupdate', function (e) {
        const progress = Player.currentTime * 100 / Player.duration;
        typeof updateCallback==="function"&&updateCallback(progress);
    });
    return function(fn){
        updateCallback=fn;
    }
}
function addEndedCallback(){
    let next=null;
    Player.addEventListener('ended', function(){
        typeof next==="function"&&next();
    })
    return function(fn){
        next=fn;
    }
}
const setUpdateCallback=addUpdateCallback();
const setEndedCallback=addEndedCallback();

export {togglePlay,changeSong,setUpdateCallback,setEndedCallback}